﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Web;
using Easy.Common;
using Easy.DAL;
using Easy.Model;
using Easy.Operation;
using Easy.Operation.Expression;
using Yahoo.Yui.Compressor;

namespace Easy.BLL
{
    public class JsHubBLL
    {
        JsHubDAL dal=new JsHubDAL();

        public String GetJsWithNameArray(List<Object> NameList)
        {
            DataService service = new DataService("jslist", Guid.Parse(Config.AdminUserId));
            FilterExpression expression = new FilterExpression()
            {
                Conditions = new List<ConditionExpression>(),
                FilterOperator = LogicalOperator.AND,
                Filters = new List<FilterExpression>()
            };
            ConditionExpression Condition=new ConditionExpression()
            {
                AttributeName = "name",Operator = ConditionOperator.In,Values = NameList
            };
            expression.Conditions.Add(Condition);
            String[] str=new String[2];
            str[0] = "JsContent".ToLower();
            str[1] = "priority".ToLower();
            var content = service.RetrieveMultiple_List(str, expression, "priority desc", 0, 10000000);
            var ContentList = content["List"] as List<Dictionary<string, object>>;
            StringBuilder builder=new StringBuilder();
            foreach (var JsContent in ContentList)
            {
                builder.Append(Decode((string)JsContent["jscontent"]));
            }
            return builder.ToString();
        }

        public String GetJsWithEntityItemName(String type,String EntityName, String ItemName)
        {
            var data = dal.GetJsWithEntityItemName(type, EntityName, ItemName);
            if (data==null)
            {
                return "";
            }
            StringBuilder builder=new StringBuilder();
            foreach (DataRow row in data.Rows)
            {
                builder.Append(Decode((string)row["JsContent"]));
                builder.Append("\n");
            }
            return builder.ToString();
        }

        public String GetCompressContent(String strNotCompressed)
        {
            strNotCompressed = Decode(strNotCompressed);
            var jsCompressor = new JavaScriptCompressor();
            jsCompressor.CompressionType = CompressionType.Standard;
            jsCompressor.Encoding = Encoding.Default;
            jsCompressor.IgnoreEval = false;
            jsCompressor.ThreadCulture = System.Globalization.CultureInfo.CurrentCulture;
            try
            {
                return jsCompressor.Compress(strNotCompressed);
            }
            catch (Exception)
            {
                return strNotCompressed;
            }
        }

        public List<JsList> GetAllJsList(string EntityName, string ItemName, string type = null)
        {
            var data=dal.GetJsListWithEntityItemName(type, EntityName, ItemName);
            return data;
        }

        public void ModifyFormJsRelation(string EntityName, string FormName, List<Guid> JsListId)
        {
            Guid id = dal.DeleteFormAllJsRelation(EntityName, FormName);
            dal.SaveFormJsRelationshipMulti(JsListId, id);
        }

        public void ModifyViewJsRelation(string EntityName, string ViewName, List<Guid> JsListId)
        {
            Guid id = dal.DeleteViewAllJsRelation(EntityName, ViewName);
            dal.SaveViewJsRelationshipMulti(JsListId, id);
        }

        private string Decode(string str)
        {
            return HttpUtility.UrlDecode(HttpUtility.HtmlDecode(str).Replace("+", "%2B"));
        }
    }
}